/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

package com.bullionxpress.server.impl;

import java.rmi.Remote;
import java.rmi.RemoteException;
import com.bullionxpress.shared.model.Quote;
import java.util.Iterator;
import com.bullionxpress.server.application.ServerGlobals;
import com.bullionxpress.shared.model.Instrument;
import com.bullionxpress.shared.util.Log;
import com.bullionxpress.shared.model.LimitOrder;
import com.bullionxpress.shared.service.NoPriceAvailableException;
import com.bullionxpress.shared.service.NoPriceAvailableException;
import com.bullionxpress.shared.model.Order;
import com.bullionxpress.shared.service.QuoteService;

/**
 *
 * @author BullionXpress.com
 */
public class QuoteManager extends java.rmi.server.UnicastRemoteObject implements QuoteService {

    public QuoteManager() throws RemoteException 
    {
        //connect();
    }

    public long getBidVolume(double min, double max) {
        if( min > max ){
            throw new IllegalArgumentException("min cannot be < than max");
        }
        long rt = 0;
        Iterator i = ServerGlobals.orders.iterator();
        while (i.hasNext()) {
            Order o = (Order) i.next();
            if (o instanceof LimitOrder && o.isOpen() && o.isBuyOrder()) {
                if (o.getPrice() >= min && o.getPrice() <= max ) {
                    rt += o.getRemainingUnits();
                }
            }
        }
        return rt;
    }

    public long getAskVolume(double min, double max) {
        if( min > max ){
            throw new IllegalArgumentException("min cannot be < than max");
        }
        long rt = 0;
        Iterator i = ServerGlobals.orders.iterator();
        while (i.hasNext()) {
            Order o = (Order) i.next();
            if (o instanceof LimitOrder && o.isOpen() && o.isSellOrder()) {
                if (o.getPrice() >= min && o.getPrice() <= max ) {
                    rt += o.getRemainingUnits();
                }
                //System.out.println("Found sell order:" + o);
            }
        }
        return rt;
    }


    public double getAsk(Instrument instrument) throws NoPriceAvailableException {
        double price = -1;
        Iterator i = ServerGlobals.orders.iterator();
        while (i.hasNext()) {
            Object obj = i.next();
            if (obj instanceof LimitOrder) {
                LimitOrder lo = (LimitOrder) obj;
                if( !lo.getInstrument().equals(instrument)){
                    continue;
                }
                if (price == -1 && lo.isOpen() && lo.isSellOrder()) {
                    price = lo.getPrice();
                }
                if (lo.isSellOrder() && lo.isOpen() && lo.getPrice() < price) {
                    price = lo.getPrice();
                }
            }
        }
        if (price == -1) {
            throw new NoPriceAvailableException();
        }
        return price;
    }

    public double getBid(Instrument instrument) throws NoPriceAvailableException {
        double price = -1;
        Iterator i = ServerGlobals.orders.iterator();
        while (i.hasNext()) {
            Object obj = i.next();

            if (obj instanceof LimitOrder) {

                LimitOrder lo = (LimitOrder) obj;
                if( !lo.getInstrument().equals(instrument)){
                    continue;
                }
                if (price == -1 && lo.isOpen() && lo.isBuyOrder()) {
                    price = lo.getPrice();
                }
                if (lo.isBuyOrder() && lo.isOpen() && lo.getPrice() > price) {
                    price = lo.getPrice();
                }
            }
        }
        if (price == -1) {
            throw new NoPriceAvailableException();
        }
        return price;
    }


    public Quote getQuote(Instrument instrument) {
        Quote q = null;
        try {
            q = new Quote();
            q.setInstrument(instrument);
            q.setBid(getBid(instrument));
            q.setAsk(getAsk(instrument));
            q.setTimestamp(System.currentTimeMillis());
        } catch (Exception ex) {
            Log.info(ex);
        }
        return q;
    }

    public void connect() {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    
}
